package edu.nf.vrs.app.controller;

import edu.nf.vrs.app.vo.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jdk.nashorn.internal.objects.annotations.Optimistic;
import org.apache.dubbo.config.annotation.DubboReference;
import org.nf.vrs.authenticate.inf.UserAuthService;
import org.nf.vrs.authenticate.inf.UserTokenService;
import org.nf.vrs.userservice.dto.UserDTO;
import org.nf.vrs.userservice.inf.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * Author: @Wai
 * Date: 2022-4-12
 */
@RestController
@Tag(name = "UserController", description = "用户服务API")
@RequestMapping("/api/vi")
public class UserController {

    @DubboReference(interfaceClass = UserService.class, version = "1.0.0",check = false)
    private UserService userService;
    @DubboReference(interfaceClass = UserAuthService.class, version = "1.0.0",check = false)
    private UserAuthService authService;
    @DubboReference(interfaceClass = UserTokenService.class, version = "1.0.0",check = false)
    private UserTokenService tokenService;

    @PostMapping("/user/login")
    @Operation(summary = "用户登录", method = "POST")
    @Parameters({@Parameter(name = "account", required = true), @Parameter(name = "password", required = true)})
    @ApiResponses({@ApiResponse(responseCode = "200", description = "认证成功返回JWT"),
            @ApiResponse(responseCode = "401", description = "认证失败", content = @Content),
            @ApiResponse(responseCode = "500", description = "服务异常", content = @Content)})
    public ResultVO login(String account, String password, HttpServletResponse response) {
        UserDTO user = authService.authenticate(account, password);
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", user.getUid());
        String token = tokenService.create(map, 2629800000L);
        response.setHeader("token", token);
        return new ResultVO(HttpStatus.OK.value());
    }

    @PostMapping("/user/personal")
    @Operation(summary = "获取用户信息", method = "POST")
    @Parameter(name = "uid", description = "用户id", required = true)
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "返回用户信息"),
            @ApiResponse(responseCode = "401", description = "查询失败"),
            @ApiResponse(responseCode = "500", description = "服务异常")
    })
    public ResultVO getUserInfo(Integer uid) {
        UserDTO user = userService.getUserById(uid);
        return new ResultVO(HttpStatus.OK.value(), "success", user);
    }
}
